#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
#include "chibi.h"
char *read;
size_t read_len;
char *err = NULL;
char *dbpath = "./data/chibis.db";
static int select_callback(void *ret, int argc, char **argv, char **azColName)
{
    int i;
    int *pret = (int *)ret;
    for (i = 0; i < argc; i++)
    {
       // printf( "%s = %s\n", azColName[i], argv[i]);
         fprintf(stderr, "%s = %s\n", azColName[i], argv[i]);
    }
   

    *pret = 1;
    return 0;
}

static int write_callback(void *NotUsed, int argc, char **argv, char **azColName)
{
    int i;
    for (i = 0; i < argc; i++)
    {
        fprintf(stderr, "%s = %s\n", azColName[i], argv[i]);
    }
    printf("\n");
    return 0;
}
int open()
{

    return 0;
}
void freedb()
{
}
// 1 exist 0 ok 2 error
int addChibi(unsigned int qino, unsigned int weij, unsigned long long int weiy, unsigned int shuj, unsigned long long int shuy, unsigned int wuj, unsigned long long int wuy)
{ // 添加用户
    int selret = select(qino);
    if (selret == 1)
        return chibiUpdateCtriJy(qino, weij, weiy, shuj, shuy, wuj, wuy);
    else if (selret == -1)
        return 2;
    else
        return write(qino, weij, weiy, shuj, shuy, wuj, wuy);
}

int chibiUpdateCtriJy(unsigned int qino, unsigned int weij, unsigned long long int weiy, unsigned int shuj, unsigned long long int shuy, unsigned int wuj, unsigned long long int wuy)
{
    return update(qino,weij,weiy,shuj,shuy,wuj,wuy);
}
int chibiUpdateVictory(unsigned int qino,unsigned int victory){
     int selret = select(qino);
    if (selret == 1)
        return updateVictory(qino,victory);
    else if (selret == -1)
        return 2;
    else
        return writeVictory(qino,victory);
}
int updateVictory(unsigned int qino,unsigned int victory){
     sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
     int ret = 0;
    char *sql;
    const char *data = "callback";
    char *tb = "chibi";
    /* Open database */
    rc = sqlite3_open(dbpath, &db);
    if (rc)
    {   
       
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        return (1);
    }
    else
    {
        fprintf(stdout, "Opened database successfully\n");
    }

    sql = "update %s set victory = %u where qino=%u;" \
     "SELECT * from chibi";
    char buf[1024];
    sprintf(buf, sql, tb, victory,qino);
    /* Execute SQL statement */
    rc = sqlite3_exec(db, buf, NULL, NULL, &zErrMsg);

    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        return 1;
    }
    else
    {
        fprintf(stdout, "Records created successfully\n");
    }
    sqlite3_close(db);
    return 0;
}
int update(unsigned int *qino, unsigned int weij, unsigned long long int weiy, unsigned int shuj, unsigned long long int shuy, unsigned int wuj, unsigned long long int wuy)
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
     int ret = 0;
    char *sql;
    const char *data = "callback";
    char *tb = "chibi";
    /* Open database */
    rc = sqlite3_open(dbpath, &db);
    if (rc)
    {   
       
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        return (1);
    }
    else
    {
        fprintf(stdout, "Opened database successfully\n");
    }

    sql = "update %s set weij = %u, weiy = %llu, shuj = %u, shuy = %llu, wuj = %u, wuy = %llu where qino=%u;" \
     "SELECT * from chibi";
    char buf[1024];
    sprintf(buf, sql, tb, weij, weiy, shuj, shuy, wuj, wuy,qino);
    /* Execute SQL statement */
    rc = sqlite3_exec(db, buf, NULL, NULL, &zErrMsg);

    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        return 1;
    }
    else
    {
        fprintf(stdout, "Records created successfully\n");
    }
    sqlite3_close(db);
    return 0;
}
int write(unsigned int qino, unsigned int weij, unsigned long long int weiy, unsigned int shuj, unsigned long long int shuy, unsigned int wuj, unsigned long long int wuy)
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;
    const char *data = "callback";
    char *tb = "chibi";
    /* Open database */
    rc = sqlite3_open(dbpath, &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        return (1);
    }
    else
    {
        fprintf(stdout, "Opened database successfully\n");
    }

    sql = "insert into %s (qino,weij,weiy,shuj,shuy,wuj,wuy) "
          "values (%u,%u,%llu,%u,%llu,%u,%llu);";
    char buf[250];
    sprintf(buf, sql, tb, qino, weij, weiy, shuj, shuy, wuj, wuy);
    /* Execute SQL statement */
    rc = sqlite3_exec(db, buf, write_callback, (void *)data, &zErrMsg);

    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        return 1;
    }
    else
    {
        fprintf(stdout, "Records created successfully\n");
    }
    sqlite3_close(db);
    return 0;
}

int writeVictory(unsigned int qino, unsigned int victory)
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;
    const char *data = "callback";
    char *tb = "chibi";
    /* Open database */
    rc = sqlite3_open(dbpath, &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        return (1);
    }
    else
    {
        fprintf(stdout, "Opened database successfully\n");
    }

    sql = "insert into %s (qino,victory)"
          "values (%u,%u);";
    char buf[250];
    sprintf(buf, sql, tb, qino, victory);
    /* Execute SQL statement */
    rc = sqlite3_exec(db, buf, write_callback, (void *)data, &zErrMsg);

    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        return 1;
    }
    else
    {
        fprintf(stdout, "Records created successfully\n");
    }
    sqlite3_close(db);
    return 0;
}


int select(int *key)
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    int selectRet;
    char *sql;
    int ret = 0;
    /* Open database */
    rc = sqlite3_open(dbpath, &db);

    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        return (1);
    }
    else
    {
        fprintf(stderr, "Opened database successfully\n");
    }
    /* Create SQL statement */
    sql = "select qino from chibi where qino=%d;";
    char buf[128];
    sprintf(buf, sql, key);
    /* Execute SQL statement */
    selectRet = sqlite3_exec(db, buf, select_callback, (void *)&ret, &zErrMsg);
    if (selectRet != SQLITE_OK)
    {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return -1;
    }
    

    fprintf(stdout, "Operation done successfully\n");
    if (ret == 1)
    {
        printf("select data not null!\n");
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return 1;
    }
    sqlite3_free(zErrMsg);
    sqlite3_close(db);
    return 0;
}
